package com.isoftstone.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoftstone.wms.common.QueryPageParam;
import com.isoftstone.wms.common.Result;
import com.isoftstone.wms.entity.Storage;
import com.isoftstone.wms.mapper.StorageMapper;
import com.isoftstone.wms.service.impl.StorageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wms
 * @since 2025-10-02
 */
@RestController
@RequestMapping("/storage")
public class StorageController {
@Resource
    private StorageService storageService;

    @PostMapping("/save")
    public Result save(@RequestBody Storage storage) {
        try {
            System.out.println("Saving storage: " + storage);
            boolean result = storageService.save(storage);
            System.out.println("Save result: " + result);
            return result ? Result.success() : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("保存失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/mod")
    public Result mod(@RequestBody Storage storage) {
        try {
            System.out.println("Updating storage: " + storage);
            boolean result = storageService.updateById(storage);
            System.out.println("Update result: " + result);
            return result ? Result.success() : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("更新失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/delete")
    public Result delete(@RequestBody HashMap<String, Object> params) {
        try {
            Integer id = (Integer) params.get("id");
            System.out.println("Deleting storage with id: " + id);
            boolean result = storageService.removeById(id);
            System.out.println("Delete result: " + result);
            return result ? Result.success() : Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("删除失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/listpage")
    public Result listpageC(@RequestBody QueryPageParam query) {
        try {
            // 获取前端传过来的查询参数
            HashMap<String, Object> param = query.getParam();
            String name = (String) param.get("name");
            // 1. 创建分页对象
            Page<Storage> page = new Page<>();
            page.setCurrent(query.getPageNum()); // 当前页码
            page.setSize(query.getPageSize());  // 每页条数
            // 2. 创建条件构造器
            LambdaQueryWrapper<Storage> wrapper = new LambdaQueryWrapper<>();
            if(StringUtils.isNotBlank(name)){
                wrapper.like(Storage::getName, name);
            }

            IPage<Storage> result = storageService.page(page, wrapper);
            return Result.success(result.getRecords(), result.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("查询失败: " + e.getMessage());
        }
    }
}